package com.example.android.common.media;

import android.media.MediaRecorder;
import android.os.Handler;

import com.example.android.utils.KCacheUtils;

import java.io.IOException;

import static android.media.MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED;


/**
 * @author lizhixian
 * @describe
 * @Version 1.0.0
 * @Created at 2019/2/22 13:32
 */
public class AudioRecorderUtil {

    MediaRecorder mMediaRecorder;
    String cacheDir = KCacheUtils.getCacheDirectory();
    private RecordEventListener recordEventListener;
    private String outputPath;

    private long startTime ;

    private Handler mHandler = new Handler();

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (recordEventListener != null) {
                int seceond = (int) ((System.currentTimeMillis() - startTime) / 1000);
                recordEventListener.onProgress(seceond);
            }
            mHandler.postDelayed(this,1000);
        }
    };

    public void startRecord() throws IOException {
        initRecord();
        startTime = System.currentTimeMillis();
        mMediaRecorder.start();
        mHandler.postDelayed(mRunnable,1000);
        if (recordEventListener != null) {
            recordEventListener.onStartRecord();
        }
    }

    private void initRecord() throws IOException {
        outputPath = cacheDir + "/" + System.currentTimeMillis() + ".amr";
        mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
        mMediaRecorder.setMaxDuration(31000);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mMediaRecorder.setAudioSamplingRate(8000);
        mMediaRecorder.setAudioChannels(1);
        mMediaRecorder.setOutputFile(outputPath);
        mMediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
            @Override
            public void onInfo(MediaRecorder mr, int what, int extra) {
                switch (what){
                    case MEDIA_RECORDER_INFO_MAX_DURATION_REACHED:
                        stopRecord();
                        break;
                }
            }
        });
        mMediaRecorder.prepare();
    }

    public void stopRecord() {
        if (mMediaRecorder != null) {
            mMediaRecorder.stop();
            mMediaRecorder.release();
            mMediaRecorder = null;
        }
        mHandler.removeCallbacks(mRunnable);
        if (recordEventListener != null) {
            int seceond = (int) ((System.currentTimeMillis() - startTime) / 1000);
            recordEventListener.onFinishedRecord(outputPath, seceond);
            recordEventListener = null;
        }
    }

    public void setRecordEventListener(RecordEventListener listener) {
        recordEventListener = listener;
    }

    public interface RecordEventListener {
        void onFinishedRecord(String audioPath, int seconds);

        void onProgress(int scecond);

        void onStartRecord();
    }
}
